<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Db
 * @package    Bushido_Db_Form
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: AssemblyLineStep.php 7 2009-11-14 22:42:08Z nkeyes $
 */

/**
 * 
 * @author Nathan Keyes
 *
 */
class Bushido_Db_Form_AssemblyLineStep implements RecursiveIterator, ArrayAccess, Countable
{
	const CHILD = 'child';
	const REFERENCE = 'reference';
	const MANY_TO_MANY = 'many to many';
	
	protected $_name;
	protected $_class;
	protected $_pairedClass;
	protected $_type;
	protected $_rule;
	protected $_pairedClassRule;
	protected $_required = true;
	protected $_id;
	protected $_dependantSteps;
	protected $_simultaneousSteps;
	
	protected $_position;
	
	
	
	public function __construct($config = array())
	{		
		$this->_position = 0;
		foreach($config as $key => $value)
		{
			$method = 'set' . ucfirst($key);
			if(method_exists($this, $method))
			{
				call_user_func(array($this, $method), $value);
			}
		}
	}
	
	public function __get($key)
	{
		$method = 'get' . ucfirst($key);
		return call_user_func(array($this, $method));
	}
	
	public function __set($key, $value)
	{
		$method = 'set' . ucfirst($key);
		return call_user_func(array($this, $method), $value);
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getName()
	{
		return $this->_name;
	}
	
	
	/**
	 * 
	 * @param $name string
	 * @return void
	 */
	public function setName($name)
	{
		$this->_name = $name;
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getClass()
	{
		return $this->_class;
	}
	
	
	/**
	 * 
	 * @param $class
	 * @return void
	 */
	public function setClass($class)
	{
		if(class_exists($class))
		{
			$this->_class = $class;
		}
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getPairedClass()
	{
		return $this->_pairedClass;
	}
	
	
	/**
	 * 
	 * @param $pairedClass
	 * @return void
	 */
	public function setPairedClass($pairedClass)
	{
		if(class_exists($pairedClass))
		{
			$this->_pairedClass = $pairedClass;
		}
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getPairedClassRule()
	{
		return $this->_pairedClassRule;
	}
	
	
	/**
	 * 
	 * @param $pairedClassRule
	 * @return void
	 */
	public function setPairedClassRule($pairedClassRule)
	{
		$this->_pairedClass = $pairedClassRule;
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getType()
	{
		return $this->_type;
	}
	
	
	/**
	 * 
	 * @param $type
	 * @return void
	 */
	public function setType($type)
	{
		$this->_type = $type;
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getRule()
	{
		return $this->_rule;
	}
	
	
	/**
	 * 
	 * @param $rule
	 * @return void
	 */
	public function setRule($rule)
	{
		$this->_rule = $rule;
	}
	
	
	/**
	 * 
	 * @return boolean
	 */
	public function isRequired()
	{
		return $this->_required;
	}
	
	
	/**
	 * 
	 * @param $required
	 * @return void
	 */
	public function setRequired($required)
	{
		$this->_required = $required;
	}
	
	
	/**
	 * 
	 * @return Zend_Db_Table_Row_Abstract
	 */
	public function getId()
	{
		return $this->_id;
	}
	
	
	/**
	 * 
	 * @param $required
	 * @return void
	 */
	public function setId($id)
	{
		$this->_id = $id;
	}

	
	/**
	 * 
	 * @param $dependantSteps Bushido_Db_Form_AssemblyLine
	 * @return unknown_type
	 */
	public function setDependantSteps(Bushido_Db_Form_AssemblyLine $dependantSteps)
	{
		$this->_dependantSteps = $dependantSteps;
	}
	
	/**
	 * 
	 * @return Bushido_Db_Form_AssemblyLine
	 */
	public function getDependantSteps()
	{
		return $this->_dependantSteps;
	}
	
	/**
	 * 
	 * @param $simultaneousSteps array
	 * @return unknown_type
	 */
	public function setSimultaneousSteps(array $simultaneousSteps)
	{
		$this->_simultaneousSteps = $simultaneousSteps;
	}
	
	/**
	 * 
	 * @return array
	 */
	public function getSimultaneousSteps()
	{
		return $this->_simultaneousSteps;
	}
	
	
	public function rewind()
	{
		$this->_position = 0;
	}

	public function current()
	{
		return $this->_dependantSteps[$this->_position];
	}
	
	public function key()
	{
		return $this->_position;
	}
	
	public function next()
	{
		$this->_position++;
	}
	
	public function valid()
	{
		return isset($this->_dependantSteps[$this->_position]);
	}
	
	public function offsetExists($key)
	{

		return isset($this->_dependantSteps[$key]);
	}
	
	public function offsetGet($key)
	{
		return $this->_dependantSteps[$key];
	}
	
	public function offsetSet($offset, $value)
	{
		if(!$value instanceof Bushido_Db_Form_AssemblyLineStep)
		{
			throw new Exception('Invalid value for dependant step, value must be an instance of Bushido_Db_Form_AssemblyLineStep');
		}
		$this->_dependantSteps[$offset] = $value;
	}
	
	public function offsetUnset($offset)
	{
		unset($this->_dependantSteps[$offset]);
	}
	
	public function hasChildren()
	{
		return count($this->_dependantSteps[$this->_position]->getDependantSteps()) > 0;
	}
	
	public function getChildren()
	{
		return $this->_dependantSteps[$this->_position]->getDependantSteps();
	}
	
	public function count()
	{
		return count($this->getDependantSteps());
	}
}
